#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main()
{
    int fd1 = open("love.txt", O_RDONLY);
    if (fd1 == -1)
    {
        perror("打开读文件失败");
        exit(EXIT_FAILURE);
    }
    int fd2 = open("love2.txt", O_WRONLY | O_CREAT | O_APPEND, 0664);
    if (fd2 == -1)
    {
        perror("打开写文件失败");
        close(fd1);
        exit(EXIT_FAILURE);
    }

    char buff[1024];
    ssize_t len;
    while ((len = read(fd1, buff, 1024)) > 0)
    {
        write(fd2, buff, len);
        write(STDOUT_FILENO, buff, len);
    }
    if (len == -1)
    {
        perror("读文件失败");
        close(fd1);
        close(fd2);
        exit(EXIT_FAILURE);
    }
    close(fd1);
    close(fd2);
    return 0;
}